package com.sysman.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sysman.HttpTools.HttpRequest;

/** 
* @author  LTT-AH-ZY
* @date 创建时间：2018年1月16日 下午12:09:28 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 
* @since  
* @return  
*/
@Controller
@RequestMapping(value="/Main/DynamicMenu")
public class DynamicMenuController {
	/**
	 * 角色管理界面初始化
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/roles")
	public ModelAndView rolesmenu()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("View/DynamicMenu/RolesMan");
		return model;
	}
	/**
	 * 角色列表
	 * @param limit
	 * @param offset
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/rolesListAjax")
	public @ResponseBody Map<String,Object> rolesListAjax(Integer limit,Integer offset,HttpServletRequest request){
		HttpSession session=request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		String param = "adminloginid="+adminloginid+"&limit="+limit+"&offset="+offset;
		List<Map<String,Object>> list = new ArrayList<>();
		Map<String,Object> result = new HashMap<>();
		Integer total=0;
		String msg="";
		String code="";
		try {
			JSONObject json = HttpRequest.sendPost("listadminrole", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
			if("100".equals(code)) {
				try {
					JSONObject data = json.getJSONObject("data");
					total = Integer.parseInt(data.get("total").toString());
					JSONArray jsonarray = data.getJSONArray("rows");
					for(int i=0;i<jsonarray.length();i++) {
						Map<String,Object> map = new HashMap<>();
						JSONObject jsondata = jsonarray.getJSONObject(i);
						map.put("roleid",jsondata.get("roleid").toString());
						map.put("rolename",jsondata.get("rolename").toString());
						map.put("rolemenuid",jsondata.get("rolemenuid").toString());
						map.put("roledescription",jsondata.get("roledescription").toString());
						list.add(map);
					}
				} catch (Exception e) {
					code="-200";
					msg="数据解析错误";
					System.out.println("出错了，错误信息是："+e.getMessage());
				}
			}
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		result.put("rows", list);
		result.put("total", total);
		
		return result;
	}
	/**
	 * 获取所有角色
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getadminrole")
	public @ResponseBody Map<String,Object> getadminrole(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid=(Integer)session.getAttribute("adminloginid");
		
		String param = "adminloginid="+adminloginid;
		List<Map<String,Object>> list = new ArrayList<>();
		Map<String,Object> result = new HashMap<>();
		String code = "";
		String msg = "";
		try {
			JSONObject json = HttpRequest.sendPost("listalladminrole", param);
			code = json.get("code").toString();
			msg = json.get("msg").toString();
			if("100".equals(code)) {
				try {
					JSONArray data = json.getJSONArray("data");
					for(int i=0;i<data.length();i++) {
						Map<String,Object> map = new HashMap<>();
						JSONObject jsondata = data.getJSONObject(i);
						map.put("roleid", jsondata.get("roleid").toString());
						map.put("rolename", jsondata.get("rolename").toString());
						map.put("rolemenuid", jsondata.get("rolemenuid").toString());
						map.put("roledescription", jsondata.get("roledescription").toString());
						list.add(map);
					}
				} catch (Exception e) {
					code="-200";
					msg="数据解析错误";
					System.out.println("出错了，错误信息是："+e.getMessage());
				}
			}
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		result.put("list", list);
		
		return result;
	}
	/**
	 * 新增角色
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/addadminrole")
	public @ResponseBody Map<String,Object> addadminrole(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		String rolename = request.getParameter("rolename");
		String roledescription = request.getParameter("roledescription");
		String code="";
		String msg="";
		
		Map<String,Object> result = new HashMap<>();
		String param = "adminloginid="+adminloginid+"&rolename="+rolename+"&roledescription="+roledescription;
		try {
			JSONObject json = HttpRequest.sendPost("addadminrole", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
			
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		
		return result;
	}
	/**
	 * 更新角色
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/updateadminrole")
	public @ResponseBody Map<String,Object> updateadminrole(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		Integer roleid=Integer.parseInt(request.getParameter("roleid"));
		String rolename = request.getParameter("rolename");
		String roledescription = request.getParameter("roledescription");
		String code="";
		String msg="";
		
		Map<String,Object> result = new HashMap<>();
		String param = "adminloginid="+adminloginid+"&roleid="+roleid+"&rolename="+rolename+"&roledescription="+roledescription;
		try {
			JSONObject json = HttpRequest.sendPost("updateadminrole", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
			
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		
		return result;
	}
	/**
	 * 删除角色
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deleterole")
	public @ResponseBody Map<String,Object> deleterole(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		Integer roleid = Integer.parseInt(request.getParameter("roleid"));
		
		String param = "adminloginid="+adminloginid+"&roleid="+roleid;
		Map<String,Object> result = new HashMap<>();
		String code="";
		String msg="";
		try {
			JSONObject json = HttpRequest.sendPost("deleteadminrole", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		
		result.put("code", code);
		result.put("msg", msg);
		
		return result;
	}
	/**
	 * 菜单管理界面初始化
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/adminmenu")
	public ModelAndView adminmenu()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("View/DynamicMenu/AdminMenu");
		return model;
	}
	/**
	 * 管理员菜单
	 * @param limit
	 * @param offset
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/listadminmenuAjax")
	public @ResponseBody Map<String,Object> listadminmenuAjax(Integer limit,Integer offset,Integer menufatherid,HttpServletRequest request)throws Exception{
		HttpSession session=request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		String param = "adminloginid="+adminloginid+"&limit="+limit+"&offset="+offset+"&menufatherid="+menufatherid;
		List<Map<String,Object>> list = new ArrayList<>();
		Map<String,Object> result = new HashMap<>();
		Integer total=0;
		String msg="";
		String code="";
		try {
			JSONObject json = HttpRequest.sendPost("listadminmenu", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
			if("100".equals(code)) {
				try {
					JSONObject data = json.getJSONObject("data");
					total = Integer.parseInt(data.get("total").toString());
					JSONArray jsonarray = data.getJSONArray("rows");
					for(int i=0;i<jsonarray.length();i++) {
						Map<String,Object> map = new HashMap<>();
						JSONObject jsondata = jsonarray.getJSONObject(i);
						map.put("menuid",jsondata.get("menuid").toString());
						map.put("menuname",jsondata.get("menuname").toString());
						map.put("menufatherid",jsondata.get("menufatherid").toString());
						map.put("menuurl",jsondata.get("menuurl").toString());
						list.add(map);
					}
				} catch (Exception e) {
					code="-200";
					msg="数据解析错误";
					System.out.println("出错了，错误信息是："+e.getMessage());
				}
			}
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		result.put("rows", list);
		result.put("total", total);
		
		return result;
	}
	/**
	 * 根据menufatherid获取一级菜单
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getadminmenu")
	public @ResponseBody Map<String,Object> getadminmenu(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid=(Integer)session.getAttribute("adminloginid");
		
		Integer menufatherid=Integer.parseInt(request.getParameter("menufatherid"));
		String param = "adminloginid="+adminloginid+"&menufatherid="+menufatherid;
		List<Map<String,Object>> list = new ArrayList<>();
		Map<String,Object> result = new HashMap<>();
		String code = "";
		String msg = "";
		try {
			JSONObject json = HttpRequest.sendPost("getadminmenu", param);
			code = json.get("code").toString();
			msg = json.get("msg").toString();
			if("100".equals(code)) {
				try {
					if(json.get("data").toString()!="null") {
						JSONArray jsondata = json.getJSONArray("data");
						for(int i=0;i<jsondata.length();i++) {
							JSONObject data = jsondata.getJSONObject(i);
							Map<String,Object> map = new HashMap<>();
							map.put("menuid", data.get("menuid").toString());
							map.put("menuname", data.get("menuname").toString());
							map.put("menufatherid", data.get("menufatherid").toString());
							map.put("menuurl", data.get("menuurl").toString());
							list.add(map);
						}
					}
				} catch (Exception e) {
					code="-200";
					msg="数据解析错误";
					System.out.println("出错了，错误信息是："+e.getMessage());
				}
			}
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		result.put("list", list);
		
		return result;
	}
	/**
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/addadminmenu")
	public @ResponseBody Map<String,Object> addadminmenu(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		String menuname = request.getParameter("menuname");
		String menufatherid = request.getParameter("menufatherid");
		String menuurl = request.getParameter("menuurl");
		String code="";
		String msg="";
		
		Map<String,Object> result = new HashMap<>();
		String param = "adminloginid="+adminloginid+"&menuname="+menuname+"&menufatherid="+menufatherid+"&menuurl="+menuurl;
		try {
			JSONObject json = HttpRequest.sendPost("addadminmenu", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
			
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		
		return result;
	}
	/**
	 * 更新菜单表数据
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/updateadminmenu")
	public @ResponseBody Map<String,Object> updateadminmenu(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		String menuid = request.getParameter("menuid");
		String menuname = request.getParameter("menuname");
		String menufatherid = request.getParameter("menufatherid");
		String menuurl = request.getParameter("menuurl");
		String code="";
		String msg="";
		
		Map<String,Object> result = new HashMap<>();
		String param = "adminloginid="+adminloginid+"&menuid="+menuid+"&menuname="+menuname+"&menufatherid="+menufatherid+"&menuurl="+menuurl;
		try {
			JSONObject json = HttpRequest.sendPost("updateadminmenu", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
			
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		
		return result;
	}
	/**
	 * 删除菜单
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deletemenu")
	public @ResponseBody Map<String,Object> deletemenu(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		Integer menuid = Integer.parseInt(request.getParameter("menuid"));
		
		String param = "adminloginid="+adminloginid+"&menuid="+menuid;
		Map<String,Object> result = new HashMap<>();
		String code="";
		String msg="";
		try {
			JSONObject json = HttpRequest.sendPost("deleteadminmenu", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		
		result.put("code", code);
		result.put("msg", msg);
		
		return result;
	}
	/**
	 * 为角色新增菜单权限
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/addadminrolemenu")
	public @ResponseBody Map<String,Object> addadminrolemenu(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		
		Integer roleid = Integer.parseInt(request.getParameter("roleid"));
		Integer menuid = Integer.parseInt(request.getParameter("menuid"));
		String param = "adminloginid="+adminloginid+"&roleid="+roleid+"&menuid="+menuid;
		String code="";
		String msg="";
		Map<String,Object> result = new HashMap<>();
		try {
			JSONObject json = HttpRequest.sendPost("addadminrolemenu", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		
		return result;
	}
	/**
	 * 管理员账号管理界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/adminlist")
	public ModelAndView adminlist()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("View/DynamicMenu/Adminlist");
		return model;
	}
	/**
	 * 
	 * @param limit
	 * @param offset
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/listadminAjax")
	public @ResponseBody Map<String,Object> listadminAjax(Integer limit,Integer offset,HttpServletRequest request)throws Exception{
		HttpSession session=request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		String param = "adminloginid="+adminloginid+"&limit="+limit+"&offset="+offset;
		List<Map<String,Object>> list = new ArrayList<>();
		Map<String,Object> result = new HashMap<>();
		Integer total=0;
		String msg="";
		String code="";
		try {
			JSONObject json = HttpRequest.sendPost("listadminaccount", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
			if("100".equals(code)) {
				try {
					JSONObject data = json.getJSONObject("data");
					total = Integer.parseInt(data.get("total").toString());
					JSONArray jsonarray = data.getJSONArray("rows");
					for(int i=0;i<jsonarray.length();i++) {
						Map<String,Object> map = new HashMap<>();
						JSONObject jsondata = jsonarray.getJSONObject(i);
						map.put("adminloginid",jsondata.get("adminloginid").toString());
						map.put("adminloginname",jsondata.get("adminloginname").toString());
						map.put("registrationtime",jsondata.get("registrationtime").toString());
						map.put("roleid",jsondata.get("roleid").toString());
						map.put("rolename",jsondata.get("rolename").toString());
						map.put("adminloginpix",jsondata.get("adminloginpix").toString());
						map.put("adminlogintime",jsondata.get("adminlogintime").toString());
						list.add(map);
					}
				} catch (Exception e) {
					code="-200";
					msg="数据解析错误";
					System.out.println("出错了，错误信息是："+e.getMessage());
				}
			}
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		result.put("rows", list);
		result.put("total", total);
		
		return result;
	}
	/**
	 * 新增管理员
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/addadminaccount")
	public @ResponseBody Map<String,Object> addadminaccount(HttpServletRequest request)throws Exception{
		HttpSession session=request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		
		String adminloginname=request.getParameter("adminloginname");
		String adminloginpwd=request.getParameter("adminloginpwd");
		String code="";
		String msg="";
		String param = "adminloginid="+adminloginid+"&adminloginname="+adminloginname+"&adminloginpwd="+adminloginpwd;
		Map<String,Object> result = new HashMap<>();
		try {
			JSONObject json = HttpRequest.sendPost("addadminaccount", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		
		return result;
	}
}
